package com.bq.gametrade.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by SONY-SONG on 2015/3/30.
 */
public class DateUtils {
    public static String formatDate(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date);
    }

    public static String formatDateChinese(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        return df.format(date);
    }

    public static Date forDate(String dateStr) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return df.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getLastWeakDate(){
            Date date = new Date();

            int year=Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
            int month=Integer.parseInt(new SimpleDateFormat("MM").format(date));
            int day=Integer.parseInt(new SimpleDateFormat("dd").format(date))-6;

            if(day<1){
                month-=1;
                if(month==0){
                    year-=1;month=12;
                }
                if(month==4||month==6||month==9||month==11){
                    day=30+day;
                }else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
                {
                    day=31+day;
                }else if(month==2){
                    if(year==0||(year %4==0&&year!=0))day=29+day;
                    else day=28+day;
                }
            }
            String y = year+"";String m ="";String d ="";
            if(month<10) m = "0"+month;
            else m=month+"";
            if(day<10) d = "0"+day;
            else d = day+"";

            return y+"-"+m+"-"+d;
        }



    public static String getDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);

        return str;
    }

    public static String getTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);

        return str;
    }

    public static String dateDiff(String startTime, String endTime, String format) {
        //按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
        long nh = 1000 * 60 * 60;//一小时的毫秒数
        long nm = 1000 * 60;//一分钟的毫秒数
        long ns = 1000;//一秒钟的毫秒数
        long diff;
        try {
            //获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
            long day = diff / nd;//计算差多少天
            long hour = diff % nd / nh;//计算差多少小时
            long min = diff % nd % nh / nm;//计算差多少分钟
            long sec = diff % nd % nh % nm / ns;//计算差多少秒
            //输出结果
            //System.out.println("时间相差：" + day + "天" + hour + "小时" + min + "分钟" + sec + "秒。");
            return String.valueOf(day);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static Date formatDateFromString(String date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

//    public static boolean compareDate(String startDate,String endDate){
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        boolean isDate;
//        try {
//            Date date_01 = sdf.parse(startDate);
//            Date date_02 = sdf.parse(endDate);
//            isDate = date_01.before(date_02);
//            System.out.println(date_01.before(date_02)); //true，当 date_01 小于 date_02 时，为 true，否则为 false
//            System.out.println(date_02.after(date_01)); //true，当 date_02 大于 date_01 时，为 true，否则为 false
//            System.out.println(date_01.compareTo(date_02)); //-1，当 date_01 小于 date_02 时，为 -1
//            System.out.println(date_02.compareTo(date_01)); //1，当 date_02 大于 date_01 时，为 1
//            System.out.println(date_02.compareTo(date_02)); //0，当两个日期相等时，为 0
//            return date_01.before(date_02);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//
//        return true;
//    }


    /**
     * 时间比较大小
     * @param s1
     * @param s2
     * @return
     */
    public static int compareDate(String s1, String s2) {
        java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
        java.util.Calendar c1 = java.util.Calendar.getInstance();
        java.util.Calendar c2 = java.util.Calendar.getInstance();
        try {
            c1.setTime(df.parse(s1));
            c2.setTime(df.parse(s2));
        } catch (java.text.ParseException e) {
            System.err.println("格式不正确");
            return -2;
        }
        int result = c1.compareTo(c2);
        if (result == 0) {
            System.out.println("c1相等c2");
            return 0;

        } else if (result < 0) {
            System.out.println("c1小于c2");
            return 1;

        } else {
            System.out.println("c1大于c2");
            return -1;
        }


    }



    public static Date stringToDate(String dateString) {
        ParsePosition position = new ParsePosition(0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date dateValue = simpleDateFormat.parse(dateString, position);
        return dateValue;
    }

    public static String getTimestamp() {
        String timestamp = DateFormat.getDateTimeInstance().format(new Date());
        return timestamp;
    }

}
